import {Component, OnInit} from '@angular/core';
import {SERVER_URL} from '../../config';
import {HttpClient} from '@angular/common/http';
import {NbToastStatus} from '@nebular/theme/components/toastr/model';
import {NbToastrService} from '@nebular/theme';

export interface ReturnData {
  ret: number;
  data: any;
  msg: string;
}

export interface Company {
  name: string;
  tel: string;
  address: string;
  id: string;
}

@Component({
  selector: 'app-company-edit',
  templateUrl: './company-edit.component.html',
  styleUrls: ['./company-edit.component.css']
})
export class CompanyEditComponent implements OnInit {
  companyInfo: Company;

  constructor(private http: HttpClient, private toastrService: NbToastrService) {
    this.loadData();
  }
  async loadData() {
    const data = await this.http.get<ReturnData>(SERVER_URL + '?s=App.Fcpn.Select').toPromise();
    if (data.data) {
      const info = data.data.code[0];
      this.companyInfo = {id: info.id, name: info.Name, tel: info.Tel, address: info.Address};
    }
  }

  submit() {
    const formData = new FormData();
    formData.append('id', this.companyInfo.id);
    formData.append('Name', this.companyInfo.name);
    formData.append('Tel', this.companyInfo.tel);
    formData.append('Address', this.companyInfo.address);
    this.http.post<ReturnData>(SERVER_URL + '?s=App.Fcpn.Update', formData).subscribe(data => {
      if (data.data.code === 1) {
        this.loadData();
        this.toastrService.show('修改成功', '提示', {status: NbToastStatus.SUCCESS});
      }else {
        this.toastrService.show('发生错误，请稍后再试', '提示', {status: NbToastStatus.WARNING});
      }
    });
  }
  ngOnInit() {
  }

}
